<?php
	class info extends common{
		private $info_table='info';				//资讯表
		private $label_table='label';			//资讯标签表
		private $info_label_table='info_label';	//资讯绑定标签
		private $info_class_table='info_class';	//资讯分类

		private $table_topic = 'admin_topic';				//小编说
		private $table_comment = 'admin_topic_comment';		//小编回复

		private $table_info_comment = 'info_comment';		//资讯回复

		private $table_user = 'user';						//会员表
		private $table_admin='admin';						//小编表
		private $user_collection_table='user_collection';	//会员收藏表

		private $city='pcc_city';							//市表

		function __construct(){
			parent::__construct();
		}

		/**
		 * 资讯分类      
		 * @return array         
		 */
		function infoClassList(){
			$where.=' order by c.id asc';
			$sql=$this->info_class_table.' c where 1=1 '.$where;
			$key='c.*';
			$list=$this->getList($sql,$key,$num=0);
			return $list;
		}

		/**
		 * 资讯标签列表
		 * @param  [type]  $where 
		 * @param  integer $num   
		 * @return [type]         
		 */
		function infoLabelList($where,$num=0){
			$sql=$this->label_table.' where 1=1 '.$where;
			$sql.=$num==0?'':' limit 0,'.$num;
			$key='*';
			$list=$this->getList($sql,$key,0);
			return $list;
		}
		
		/**
		 * 资讯列表
		 * @param  string  $where 
		 * @param  string  $key   
		 * @param  integer $num 	分页：每页数量  
		 * @return array         
		 */
		function infoList($where='',$key='i.*',$num=0){
			$sql=$this->info_table.' i
				  where 1=1 '.$where;
			$key.='';
			$list=$this->getList($sql,$key,$num);
			$class=$this->infoClassList();				//资讯分类
			if(is_array($list['result'])){
				foreach($list['result'] as $k=>$v){
					if(is_array($class['result'])){
						foreach($class['result'] as $key=>$val){
							if($v['class_id']==$val['id']){
								$list['result'][$k]['className']=$val['class_name'];
							}
						}
					}
				}
			}
			return $list;
		}

		/**
		 * 根据id获得资讯详情
		 * @param  int $info_id 
		 * @param  string $key     
		 * @return array          
		 */
		function infoDetail($info_id,$key='i.*'){
			$sql=$this->info_table.' i where i.id="'.$info_id.'"';
			$info=$this->getInfo($sql,$key);
			return $info;
		}	

		/**
		 * 资讯所绑定的标签
		 * @param  string  $where 
		 * @param  string  $key   
		 * @param  integer $join  
		 * @return array
		 * @author wxy         
		 */
		function infoLabel($where='',$key='*',$join=0){
			$sql=$this->info_label_table.' il ';
			$sql.=$join==0?'':' left join '.$this->label_table.' l on il.label_id=l.id ';
			$sql.=' where 1=1 '.$where;
			$list=$this->getList($sql,$key,0);
			return $list;
		}

		/**
		 * 资讯投稿
		 * @param  array $arr 
		 * @return int   $id
		 * @author wxy
		 */
		function infoApplyAdd($arr){
			if(!empty($arr)){
				$res=$this->insert($this->info_table,$arr);
				$id=$this->insert_id();
			}
			return $id;
		}

		/**
		 * 资讯更新
		 * @param  array $arr   
		 * @param  string $where 
		 * @return bool
		 * @author wxy        
		 */
		function infoUpdate($arr,$where=''){
			$res=$this->update($this->info_table,$arr,$where);
			return $res;
		}

		/**
		 * 批量资讯绑定标签
		 * @param  [type] $arr_id  
		 * @param  [type] $info_id 
		 * @return bool
		 * @author wxy          
		 */
		function infoLabelAdd($arr_id,$info_id){
			if(is_array($arr_id)){
				$sql='INSERT INTO '.$this->info_label_table.' (id,info_id,label_id) VALUES ';
				foreach($arr_id as $k=>$v){
					if(is_array($v)){
						foreach($v as $key=>$val){
							$lid=$key;
							$sql.='('.$lid.','.$info_id.','.$val.'),';
						}
					}
				}
				$sql=substr($sql,0,-1);
				$sql.=' ON duplicate key update label_id=values(label_id)';
				$res=$this->query($sql);
			}
			return $res;
		}

		/**
		 * 资讯作者信息
		 * @param  string $type  
		 * @param  string $where 
		 * @param  string $key   
		 * @return array  
		 * @author wxy      
		 */
		function adminInfo($type='user',$where='',$key='a.*'){
			$table=$type=='user'?$this->table_user:$this->table_admin;
			$sql=$table.' a where 1=1 '.$where;
			$key.=',(select city from '.$this->city.' where cityid=a.city) as cityName';
			$info=$this->getInfo($sql,$key);
			return $info;
		}

		/**
		 * 小编说列表
		 * @param	string	$where  
		 * @param	string	$key 
		 * @param	int	$is_page  
		 * @return 	array
		 */
		function topicList($where='',$key="a.*,u.name,u.avatar",$num=0,$order="a.create_time desc"){
			$sql = $this->table_topic." a LEFT JOIN ".$this->table_admin." u ON a.admin_id=u.id WHERE 1=1 ".$where." order by ".$order ;
			$res=$this->getList($sql,$key,$num);
			return $res ;
		}

		/**
		 * 小编说右侧吐槽排行
		 * @author	jsy 
		 * @param	string	$where  
		 * @param	string	$key 
		 * @param	int	$is_page  
		 * @return 	array
		 */
		function topicRight($where='',$key="a.*,u.name,u.avatar",$num=0){
			$sql = $this->table_topic." a LEFT JOIN ".$this->table_admin." u ON a.admin_id=u.id WHERE 1=1 ".$where." order by topicNum desc" ;
			$res=$this->getList($sql,$key,$num);
			return $res ;
		}



		/**
		 * 小编说评论
		 * @author	jsy 
		 * @param	string	$where  
		 * @param	string	$key 
		 * @param	int	$is_page  
		 * @return 	array
		 */
		function commentList($where='',$key="a.*",$num=0){
			$sql = $this->table_comment." a LEFT JOIN ".$this->table_user." u ON a.user_id=u.id LEFT JOIN ".$this->table_user." u1 ON a.reply_id=u1.id WHERE 1=1 ".$where." order by a.id desc" ;
			$res=$this->getList($sql,$key,$num);
			return $res ;
		}

		/**
		 * 资讯评论
		 * @author	jsy 
		 * @param	string	$where  
		 * @param	string	$key 
		 * @param	int	$is_page  
		 * @return 	array
		 */
		function infoComment($where='',$key="*",$num='10'){
			$sql = $this->table_info_comment." a LEFT JOIN ".$this->table_user." u ON a.user_id=u.id LEFT JOIN ".$this->table_user." u1 ON a.reply_id=u1.id LEFT JOIN ".$this->table_admin." m ON a.user_id=m.id WHERE 1=1  ".$where." order by a.create_time desc" ;
			$res=$this->getList($sql,$key,$num);
			return $res ;
		}

		/**
		 * 会员收藏资讯列表
		 * @param  string  $key 
		 * @param  integer $num 
		 * @return array 
		 * @author wxy      
		 */
		function infoCollectList($key="*",$num=0){
			$user_id=$_COOKIE['user_id'];			//会员id
			$sql=$this->user_collection_table.' c left join '.$this->info_table.' i on c.type_id=i.id 
				  where c.user_id="'.$user_id.'" 
				  and c.type="info" order by c. create_time desc';
			$list=$this->getList($sql,$key,$num);
			return $list;
		}

		/**
		 * 删除收藏
		 * @param  int $id        
		 * @param  string $searchKey 
		 * @return bool  
		 * @author wxy          
		 */
		function collectDel($id,$searchKey='id'){
			$res=$this->del($this->user_collection_table,$id.',','',$searchKey);
			return $res;
		}


		
	}
?>